package com.ruoyi.employee.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.employee.domain.Employee;
import com.ruoyi.employee.mapper.EmployeeMapper;
import com.ruoyi.employee.service.IEmployeeService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 医护人员Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    /**
     * 查询医护人员
     * 
     * @param id 医护人员主键
     * @return 医护人员
     */
    @Override
    public Employee selectEmployeeById(Long id) {
        return employeeMapper.selectEmployeeById(id);
    }

    /**
     * 查询医护人员列表
     * 
     * @param employee 医护人员
     * @return 医护人员
     */
    @Override
    public List<Employee> selectEmployeeList(Employee employee) {
        return employeeMapper.selectEmployeeList(employee);
    }

    /**
     * 新增医护人员
     * 
     * @param employee 医护人员
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEmployee(Employee employee) {
        employee.setCreateTime(DateUtils.getNowDate());
        employee.setUpdateTime(DateUtils.getNowDate());
        int rows = employeeMapper.insertEmployee(employee);

        // 创建对应的用户账号
        if (StringUtils.isNotEmpty(employee.getUser())) {
            SysUser sysUser = new SysUser();
            sysUser.setUserName(employee.getUser());
            sysUser.setNickName(employee.getName());
            sysUser.setPassword("123456"); // 默认密码
            sysUser.setStatus("0"); // 正常状态
            sysUser.setDelFlag("0"); // 未删除
            userService.insertUser(sysUser);

            // 获取医生角色
            SysRole role = new SysRole();
            role.setRoleKey("doctor");
            List<SysRole> roles = roleService.selectRoleList(role);
            if (!roles.isEmpty()) {
                // 分配医生角色
                userService.insertUserAuth(sysUser.getUserId(), new Long[] { roles.get(0).getRoleId() });
            }
        }

        return rows;
    }

    /**
     * 修改医护人员
     * 
     * @param employee 医护人员
     * @return 结果
     */
    @Override
    public int updateEmployee(Employee employee) {
        employee.setUpdateTime(DateUtils.getNowDate());
        return employeeMapper.updateEmployee(employee);
    }

    /**
     * 批量删除医护人员
     * 
     * @param ids 需要删除的医护人员主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEmployeeByIds(Long[] ids) {
        // 获取所有要删除的医护人员信息
        for (Long id : ids) {
            Employee employee = employeeMapper.selectEmployeeById(id);
            if (employee != null && StringUtils.isNotEmpty(employee.getUser())) {
                // 删除对应的用户账号
                SysUser sysUser = userService.selectUserByUserName(employee.getUser());
                if (sysUser != null) {
                    userService.deleteUserById(sysUser.getUserId());
                }
            }
        }

        return employeeMapper.deleteEmployeeByIds(ids);
    }

    /**
     * 删除医护人员信息
     * 
     * @param id 医护人员主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEmployeeById(Long id) {
        // 获取医护人员信息
        Employee employee = employeeMapper.selectEmployeeById(id);
        if (employee != null && StringUtils.isNotEmpty(employee.getUser())) {
            // 删除对应的用户账号
            SysUser sysUser = userService.selectUserByUserName(employee.getUser());
            if (sysUser != null) {
                userService.deleteUserById(sysUser.getUserId());
            }
        }

        return employeeMapper.deleteEmployeeById(id);
    }

    /**
     * 根据用户名查询医护人员信息
     * 
     * @param username 用户名
     * @return 医护人员信息
     */
    @Override
    public Employee selectEmployeeByUsername(String username) {
        return employeeMapper.selectEmployeeByUsername(username);
    }
}
